package com.innjia.sys.controller;

import com.innjia.base.common.Constant;
import com.innjia.base.page.Page;
import com.innjia.base.utils.PageUtils;
import com.innjia.base.utils.Query;
import com.innjia.base.utils.Result;
import com.innjia.sys.entity.UserEntity;
import com.innjia.sys.service.UserRoleService;
import com.innjia.sys.service.UserService;
import com.innjia.utils.ShiroUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;


/**
 * 系统用户表
 *
 * @author innjia
 * @email huangxianyuan@gmail.com
 * @date 2017-05-03 09:41:38
 */
public abstract class AbstractUserController extends BaseController {

    /**
     * 列表
     */
    public Result list(@RequestParam Map<String, Object> params, Consumer<UserEntity> consumer) {
        //查询列表数据
        Query query = new Query(params);
        Page userList = getUserService().queryList(query);
        List list = userList.getResult();
        //脱敏
        list.forEach(obj -> {
            UserEntity userEntity = (UserEntity) obj;
            if (null != consumer) {
                consumer.accept(userEntity);
            }
        });
        PageUtils pageUtil = new PageUtils(userList.getResult(), userList.getTotal(), userList.getPageSize(), userList.getPageNum());
        return Result.ok().put("page", pageUtil);
    }

    /**
     * 获取service
     *
     * @return
     */
    protected abstract UserService getUserService();

    /**
     * 信息
     */
    public Result info(String id, Consumer<UserEntity> consumer) {
        UserEntity user = getUserService().queryObject(id);
        if (user != null && null != consumer) {
            consumer.accept(user);
        }
        return Result.ok().put("user", user);
    }

    /**
     * 主页用户信息
     */
    public Result info(Consumer<UserEntity> consumer) {
        UserEntity userEntity = ShiroUtils.getUserEntity();
        if (null != userEntity) {
            UserEntity user = getUserService().queryObject(ShiroUtils.getUserId());
            if (user != null) {
                user.setRoleIdList(getUserRoleService().queryRoleIdList(user.getId()));
                if (null != consumer) {
                    consumer.accept(user);
                }
            }
            return Result.ok().put("user", user);
        } else {
            return Result.error();
        }
    }

    /**
     * 获取service
     *
     * @return
     */
    protected abstract UserRoleService getUserRoleService();

    /**
     * 保存
     */
    public Result save(UserEntity user) {
        try {
            getUserService().save(user);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("手机号已存在");
        }
    }

    /**
     * 修改
     */
    public Result update(UserEntity user, Consumer<UserEntity> consumer) {
        user.setPassWord(null);
        if (null != consumer) {
            consumer.accept(user);
        }
        try {
            getUserService().update(user);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("用户手机号已被其它人员绑定");
        }
    }

    /**
     * 修改密码
     */
    public Result updatePassword(UserEntity user) {
        int i = getUserService().updatePassword(user);
        if (i < 1) {
            return Result.error("更改密码失败");
        }
        return Result.ok("更改密码成功");
    }

    /**
     * 禁用、
     */
    public Result delete(String[] ids) {
        getUserService().updateBatchStatus(ids, Constant.ABLE_STATUS.NO.getValue());
        return Result.ok();
    }

    /**
     * 启用、
     */
    public Result enabled(String[] ids) {
        getUserService().updateBatchStatus(ids, Constant.ABLE_STATUS.YES.getValue());
        return Result.ok();
    }

    /**
     * 重置密码
     */
    public Result reset(String[] ids) {
        getUserService().resetPassWord(ids);
        return Result.ok("重置密码成功,密码为:" + Constant.DEF_PASSWORD);
    }

}
